PIC24F
IC Peripheral Module Library Help


Table Of Contents

1     Library Features. 3

2     Using Library Functions in Your Code. 3

3     Functions. 6

3.1     CloseCaptureX (X = 1 … 5 ) 6

3.2     ConfigIntCaptureX (X = 1 … 5 ) 6

3.3     OpenCaptureX (X = 1 … 5 ) 6

3.4     ReadCaptureX (X = 1 … 5 ) 7

4     Macros. 7

4.1     EnableIntICX (X = 1 … 5 ) 7

4.2     DisableIntICX (X = 1 … 5 ) 7

4.3     SetPriorityIntICX (X = 1 … 5 ) 8

 


1          Library Features

 

This peripheral library module:

 

·         Capture a timer value from one of two selectable time bases upon an event on an input pin.

·         Has a four-level FIFO buffer. The number of capture events required to generate a CPU interrupt can be selected by the user.

·         Has multiple operating modes which are selected via the ICxCON register.

 

2          Using Library Functions in Your Code

 

Library routine parameters can be constructed using either AND based mask or AND_OR based mask setting. For more information on these masks, see 16-bit Peripheral Libraries.

 

Examples of use for both the methods are below:

 

Example of Use ( AND mask )

 

#include<inCap.h>

int Interrupt_Count = 0 , Int_flag, count;

unsigned int timer_first_edge, timer_second_edge;

void __attribute__((__interrupt__)) _IC1Interrupt(void)

{

   Interrupt_Count++;

   if(Interrupt_Count == 1)

   ReadCapture1(&timer_first_edge);

   else if(Interrupt_Count == 2)

   ReadCapture1(&timer_second_edge);

   Int_flag = 1;

   IFS0bits.IC1IF = 0;

}

 

int main(void)

{

   unsigned int period;

   Int_flag = 0;

   TRISDbits.TRISD0 = 0; /* Alarm output on RD0 */

   PORTDbits.RD0 = 1;

/* Enable Timer1 Interrupt and Priority to '1' */

   ConfigIntCapture1(IC_INT_PRIOR_1 & IC_INT_ON);

   T3CON = 0x8000; /* Timer 3 On */

/* Configure the InputCapture to stop in idle mode , Timer 3 as source ,

   interrupt on capture 1, I/C on every falling edge */

   OpenCapture1(IC_IDLE_STOP & IC_TIMER3_SRC &

                IC_INT_1CAPTURE & IC_EVERY_FALL_EDGE);

   while(1)

   {

      while(!Int_flag); /* wait here till first capture event */

      Int_flag = 0;

      while(!Int_flag); /* wait here till next capture event */

   /* calculate time count between two capture events */

      period = timer_second_edge - timer_first_edge;

   /* if the time count between two capture events is more than

      0x200 counts, set alarm on RD0 */

      if(period >= 0x200)

      {

      /* set alarm and wait for sometime and clear alarm */

         PORTDbits.RD0 = 0;

         while(count <= 0x10)

         {

            count++;

         }

         PORTDbits.RD0 = 1;

      }

      Interrupt_Count = 0;

      count = 0;

   }

   CloseCapture1();

}

 

Example of Use ( AND_OR mask )

 

#define USE_AND_OR        /* To enable AND_OR mask setting */

#include<inCap.h>

int Interrupt_Count = 0 , Int_flag, count;

unsigned int timer_first_edge, timer_second_edge;

void __attribute__((__interrupt__)) _IC1Interrupt(void)

{

   Interrupt_Count++;

   if(Interrupt_Count == 1)

   ReadCapture1(&timer_first_edge);

   else if(Interrupt_Count == 2)

   ReadCapture1(&timer_second_edge);

   Int_flag = 1;

   IFS0bits.IC1IF = 0;

}

int main(void)

{

   unsigned int period;

   Int_flag = 0;

   TRISDbits.TRISD0 = 0; /* Alarm output on RD0 */

   PORTDbits.RD0 = 1;

/* Enable Timer1 Interrupt and Priority to '1' */

   ConfigIntCapture1(IC_INT_PRIOR_1 | IC_INT_ON);

   T3CON = 0x8000; /* Timer 3 On */

/* Configure the InputCapture in stop in idle mode , Timer 3 as source ,

   interrupt on capture 1, I/C on every fall edge */

   OpenCapture1(IC_IDLE_STOP | IC_TIMER3_SRC |

   IC_INT_1CAPTURE | IC_EVERY_FALL_EDGE);

   while(1)

   {

      while(!Int_flag); /* wait here till first capture event */

      Int_flag = 0;

      while(!Int_flag); /* wait here till next capture event */

      /* calculate time count between two capture events */

      period = timer_second_edge - timer_first_edge;

      /* if the time count between two capture events is more than

         0x200 counts, set alarm on RD0 */

      if(period >= 0x200)

      {

      /* set alarm and wait for sometime and clear alarm */

         PORTDbits.RD0 = 0;

         while(count <= 0x10)

         {

            count++;

         }

         PORTDbits.RD0 = 1;

      }

      Interrupt_Count = 0;

      count = 0;

   }

   CloseCapture1();

}

 

3          Functions

3.1         CloseCaptureX (X = 1 … 5 )

 

Function Prototype

void CloseCapture1(void);

void CloseCapture2(void);

void CloseCapture3(void);

void CloseCapture4(void);

void CloseCapture5(void);

Include

incap.h

Description

This function turns off the Input Capture module

Arguments

None

Return Value

None

Remarks:

This function disables the Input Capture interrupt and then turns off the module. The Interrupt Flag bit is also cleared.

3.2         ConfigIntCaptureX (X = 1 … 5 )

 

Function Prototype

void ConfigIntCapture1(unsigned int config);

void ConfigIntCapture2(unsigned int config);

void ConfigIntCapture3(unsigned int config);

void ConfigIntCapture4(unsigned int config);

void ConfigIntCapture5(unsigned int config);

Include

incap.h

Description

This function configures the Input Capture interrupt.

Arguments

config - Input Capture interrupt priority and enable/disable information as defined below:

Interrupt enable/disable

   IC_INT_ON

   IC_INT_OFF

Interrupt Priority

   IC_INT_PRIOR_0

   IC_INT_PRIOR_1

   IC_INT_PRIOR_2

   IC_INT_PRIOR_3

   IC_INT_PRIOR_4

   IC_INT_PRIOR_5

   IC_INT_PRIOR_6

   IC_INT_PRIOR_7

Return Value

None

Remarks:

This function clears the Interrupt Flag bit and then sets the interrupt priority and enables/disables the interrupt.

3.3         OpenCaptureX (X = 1 … 5 )

 

Function Prototype

void OpenCapture1(unsigned int config);

void OpenCapture2(unsigned int config);

void OpenCapture3(unsigned int config);

void OpenCapture4(unsigned int config);

void OpenCapture5(unsigned int config);

Include

incap.h

Description

This function configures the Input Capture module.

Arguments

config - This contains the parameters to be configured in the ICxCON register as defined below:

Idle mode operation

   IC_IDLE_CON

   IC_IDLE_STOP

Clock select

   IC_TIMER2_SRC

   IC_TIMER3_SRC

Captures per interrupt

   IC_INT_4CAPTURE

   IC_INT_3CAPTURE

   IC_INT_2CAPTURE

   IC_INT_1CAPTURE

   IC_INTERRUPT

IC mode select

   IC_EVERY_EDGE

   IC_EVERY_16_RISE_EDGE

   IC_EVERY_4_RISE_EDGE

   IC_EVERY_RISE_EDGE

   IC_EVERY_FALL_EDGE

   IC_INPUTCAP_OFF

Return Value

None

Remarks:

This function disables the Input Capture interrupt and then turns off the module. The Interrupt Flag bit is also cleared.

3.4         ReadCaptureX (X = 1 … 5 )

Function Prototype

void CloseCapture1(void);

void CloseCapture2(void);

void CloseCapture3(void);

void CloseCapture4(void);

void CloseCapture5(void);

Include

incap.h

Description

This function reads all the pending Input Capture buffers.

Arguments

buffer - This is the pointer to the locations where the data read from the Input Capture buffers have to be stored.

Return Value

None

Remarks:

This function reads all the pending Input Capture buffers until the buffers are empty indicated by the ICxCON<ICBNE> bit getting cleared.

 

4          Macros

4.1         EnableIntICX (X = 1 … 5 )

Macro

EnableIntIC1

EnableIntIC2

EnableIntIC3

EnableIntIC4

EnableIntIC5

Include

incap.h

Description

This macro enables the timer interrupt.

Arguments

None

Remarks

This macro sets Input Capture Interrupt Enable bit of Interrupt Enable Control register.

 

4.2         DisableIntICX (X = 1 … 5 )

Macro

DisableIntIC1

DisableIntIC2

DisableIntIC3

DisableIntIC4

DisableIntIC5

Include

incap.h

Description

This macro disables the interrupt on capture event.

Arguments

None

Remarks

This macro clears Input Capture Interrupt Enable bit of Interrupt Enable Control register.

 

4.3         SetPriorityIntICX (X = 1 … 5 )

Macro

SetPriorityIntIC1

SetPriorityIntIC2

SetPriorityIntIC3

SetPriorityIntIC4

SetPriorityIntIC5

Include

incap.h

Description

This macro sets priority for input capture interrupt.

Arguments

priority

Remarks

This macro sets Input Capture Interrupt Priority bits of Interrupt Priority Control register.